package com.gosophia.conferenceManagement.common.ws.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.gosophia.commons.DozerChangeUtil;
import com.gosophia.commons.exception.BusinessException;
import com.gosophia.conferenceManagement.common.dto.ConferenceJobDataDTO;
import com.gosophia.conferenceManagement.common.entity.ConferenceJobData;
import com.gosophia.conferenceManagement.common.service.ConferenceHandleService;
import com.gosophia.conferenceManagement.common.service.ConferenceQueryService;
import com.gosophia.conferenceManagement.common.ws.ConferenceMissionDetailWebService;
import com.gosophia.conferenceManagement.starting.dto.ConferenceStartingDTO;
import com.gosophia.conferenceManagement.starting.entity.ConferenceApplicationJobData;
import com.gosophia.conferenceManagement.starting.entity.ConferenceStartingJobData;
import com.gosophia.conferenceManagement.starting.service.ConferenceStartingService;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.entity.Job;
import com.gosophia.taskManagement.entity.Mission;
import com.gosophia.taskManagement.service.TaskManagementService;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.gosophia.userManagement.entity.UserInfo;
/**
 * 会议详细Detail WebServiceImpl
 * @author wangxinbing
 * @since 2011-04-06
 * Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Service("conferenceMissionDetailWebService")
public class ConferenceMissionDetailWebServiceImpl implements
		ConferenceMissionDetailWebService {
	@Autowired
	private TaskManagementService taskManagementService;
	@Autowired
	private ConferenceStartingService conferenceStartingService;
	@Autowired
	private ConferenceHandleService conferenceHandleService;
	@Autowired
	private ConferenceQueryService conferenceQueryService;
	@Autowired
    private  Mapper dozer ;
	@Override
	public ConferenceStartingDTO findConferenceMissionDetailByMissionId(
			Long missionId) throws BusinessException {
		Assert.notNull(missionId,"missionId不能为null");
		Mission mission = taskManagementService.findMissionDetailByMissionId(missionId);
		// ConferenceStartingJobData任务信息
		ConferenceStartingJobData conferenceStartingJobData = new ConferenceStartingJobData();
		ConferenceApplicationJobData conferenceApplicationJobData = new ConferenceApplicationJobData();
		if(mission.getFirstJob() !=null){
			if(mission.getFirstJob().getJobId()!=null){
				conferenceStartingJobData = (ConferenceStartingJobData) taskManagementService
						.findJobDataDetail(mission.getFirstJob().getJobId(),
								ConferenceStartingJobData.class);
			}
			// ConferenceApplicationJobData申请信息
			if(mission.getFirstJob().getNextJob() !=null &&mission.getFirstJob().getNextJob().getJobId() !=null){
			conferenceApplicationJobData = conferenceStartingService.getNewApplicationJobData(mission.getMissionId());
			}
		}
		ConferenceStartingDTO conferenceStartingDTO = new ConferenceStartingDTO();
		//发起信息
		if(conferenceStartingJobData !=null){
			//设置Mission信息
			setMissionDetailInfo(mission, conferenceStartingJobData,
					conferenceStartingDTO);
			//申请信息
			setConferenceApplicationInfo(conferenceApplicationJobData,
					conferenceStartingDTO);
		}else{
			setMissionDetailInfo(mission, null,
					conferenceStartingDTO);
		}
		
		return conferenceStartingDTO;
	}
	/**
	 * 设置会议申请信息
	 * @param conferenceApplicationJobData
	 * @param conferenceStartingDTO
	 */
	private void setConferenceApplicationInfo(
			ConferenceApplicationJobData conferenceApplicationJobData,
			ConferenceStartingDTO conferenceStartingDTO) {
		if(conferenceApplicationJobData !=null){
			conferenceStartingDTO.setPlanStartTime(conferenceApplicationJobData
					.getPlanStartTime());
			conferenceStartingDTO.setMajorIssue(conferenceApplicationJobData
					.getMajorIssue());
			conferenceStartingDTO
					.setMajorParticipators(conferenceApplicationJobData
							.getMajorParticipators());
			conferenceStartingDTO.setApplicationInfo(conferenceApplicationJobData
					.getApplicationInfo());
			if(conferenceApplicationJobData.getConfernceUnderTaker()!=null){
				conferenceStartingDTO
						.setConfernceUnderTakerId(conferenceApplicationJobData
								.getConfernceUnderTaker().getUserInfoId());
				conferenceStartingDTO
						.setConfernceUnderTakerName(conferenceApplicationJobData
								.getConfernceUnderTaker().getUserName());
			}
		}
	}
	/**
	 * 设置mission信息
	 * @param mission
	 * @param conferenceStartingJobData
	 * @param conferenceStartingDTO
	 */
	private void setMissionDetailInfo(Mission mission,
			ConferenceStartingJobData conferenceStartingJobData,
			ConferenceStartingDTO conferenceStartingDTO) {
		conferenceStartingDTO.setConferenceName(mission.getMissionName());
		conferenceStartingDTO.setMissionCode(mission.getMissionCode());
		conferenceStartingDTO.setImportentLevel(mission.getImportance().getDataValueId());
		conferenceStartingDTO
				.setConferenceDescription(mission.getDescription());
		conferenceStartingDTO.setPlanEndTime(mission.getScheduleEndDate());
		if(conferenceStartingJobData !=null){
			conferenceStartingDTO.setConferenceType(conferenceStartingJobData
					.getConferenceType());
			 //新增conferenceModel
	        conferenceStartingDTO.setConferenceModel(conferenceStartingJobData.getConferenceModel());
		}else{
			conferenceStartingDTO.setConferenceType(null);
		}

	}
	@Override
	public ConferenceJobDataDTO findConferenceJobDataByJobId(Long jobId) throws BusinessException {
		ConferenceJobData conferenceJobData = (ConferenceJobData)taskManagementService.
		                           findJobDataDetail(jobId, ConferenceJobData.class);
		ConferenceJobDataDTO conferenceJobDataDTO = new ConferenceJobDataDTO();
		if(conferenceJobData !=null){
			conferenceJobDataDTO = DozerChangeUtil.changeBeforeToAfter(conferenceJobData, ConferenceJobDataDTO.class);
			if(conferenceJobDataDTO.getNextApproval()==null && conferenceJobData.getNextApproval()!=null){
				UserInfoDTO userInfo = DozerChangeUtil.changeBeforeToAfter(conferenceJobData.getNextApproval(), UserInfoDTO.class);
				conferenceJobDataDTO.setNextApproval(userInfo);
			}
		}
		return conferenceJobDataDTO;
	}
    @Override
    public List<JobDTO> findJobByJobIdAndJobType(Long jobId,
            String jobType) throws BusinessException {
        Assert.notNull(jobId,"jobId不能为null");
        Assert.notNull(jobType,"jobType不能为null");
        Job job = taskManagementService.findJobDetailByJobId(jobId);
        Long missionId = job.getMission().getMissionId();
        List<Job> finishedHistoryList = taskManagementService
                .findJobByMissionTypeAndJobTypeFinished(missionId, jobType);
        List<JobDTO> historyDTOs = new ArrayList<JobDTO>();
        if (finishedHistoryList != null && finishedHistoryList.size() != 0) {
            for (Job j : finishedHistoryList) {
                JobDTO jDTO = DozerChangeUtil.changeBeforeToAfter(j,
                        JobDTO.class);
                UserInfo user = j.getOperator();
                UserInfoDTO approval = DozerChangeUtil.changeBeforeToAfter(
                        user, UserInfoDTO.class);
                jDTO.setOperatorDTO(approval);
                historyDTOs.add(jDTO);
            }
        }
        return historyDTOs;
    }

}
